﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Web;
using MvcEngine.Core.Localization;

namespace MvcEngine.Core.Configuration
{
    public class ConfigurationUtil : IConfigurationUtil
    {
        private MeConfigurationSection mvcEngineSection;

        public ConfigurationUtil()
        {
            mvcEngineSection = WebConfigurationManager.GetWebApplicationSection("MvcEngineConfiguration")
                                                as MeConfigurationSection;
        }

        public static IConfigurationUtil Current
        {
            get { return IoC.Resolve<IConfigurationUtil>(); }
        }

        public virtual string[] GetUnauthenticatedPages()
        {
            List<string> result = new List<string>();
            foreach (PageConfigurationElement item in mvcEngineSection.Permissions.UnauthenticatedPages)
                result.Add(item.Page);
            return result.ToArray();
        }

        public virtual string ConnectionString
        {
            get { return WebConfigurationManager.ConnectionStrings["MvcEngineConnectionString"].ConnectionString; }
        }

        public virtual string TempFolder
        {
            get
            {
                return GetPath("Temp");
            }
        }

        public virtual string ModulesFolder
        {
            get
            {

                return GetPath("Modules");
            }
        }

        public virtual string ModulesFolderVirtualPath
        {
            get { return mvcEngineSection.Folders["Modules"]; }
        }

        public virtual string SkinsFolder
        {
            get
            {
                return GetPath("Skins");
            }
        }

        public virtual string ScriptsPath
        {
            get { return mvcEngineSection.Folders["Scripts"]; }
        }

        public virtual string CssPath
        {
            get { return mvcEngineSection.Folders["Css"]; }
        }

        public virtual string DataProvider
        {
            get { return mvcEngineSection.DataProvider.Value; }
        }

        public virtual string BinFolder
        {
            get
            {
                return HttpContext.Current.Server.MapPath("~/Bin");
            }
        }

        private string GetPath(string name)
        {
            return HttpContext.Current.Server.MapPath(mvcEngineSection.Folders[name]);
        }

        public virtual List<LanguageInfo> Languages
        {
            get
            {
                List<LanguageInfo> result = new List<LanguageInfo>();
                foreach (LanguageConfigurationElement item in mvcEngineSection.Languages)
                {
                    result.Add(new LanguageInfo() { Name = item.Name, ShortName = item.ShortName });
                }

                return result;
            }
        }

        public virtual string ImagesPath
        {
            get { return mvcEngineSection.Folders["Images"]; }
        }

        public ExtensionConfigurationCollection Extensions
        {
            get
            {
                return mvcEngineSection.Extensions;
            }
        }
    }
}
